I believe the following is the best way to work with Nagle's algorithm / TCP_NODELAY
/ TCP_CORK
.
It is described in this RedHat manual and the verdict is:
- Set
TCP_NODELAY = 1
, always. - If you can batch data for sending by creating a buffer manually, or using
writev()
, prefer that. - If you cannot (e.g. "when using different libraries that provides abstractions for layers" from the above manual):
- Set
TCP_CORK = 1
, then write the data, then setTCP_CORK = 0
.
- Set
- This builds a packet in kernel space and then flushes it out.